/**
 * 20100421 by wind yao
 */
if (!Util){
    var Util={};
}

Util.Validator=function(callbacks){
    this.Patrn={
        Required:/.+/,// required
        Number:/^\d*$/,// nothing or number
        Integer:/^[-\+]?\d+$/,// integer
        Pint:/^\\d*$/,//positive integer
        Double:/^[-\+]?\d+(\.\d+)?$/,//double
        Character:/^\w*$/,//only letters are allowed.
        NoPun:/^[A-Za-z0-9 ]*$/,//only letters, number and space are allowed.
        Email:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,//email  
        Password:/^(\S){6,15}$/,//password
//        ZipCode:/^[1-9]\d{5}$/,
        ZipCode:/^[a-zA-Z0-9 ]{3,12}$/,//zipcode
        Phone:/^[A-Za-z0-9-\(\) ]*$/,
		TickerName:/^[a-zA-Z0-9 @!:.%&_\/\-\$]+$/,
		Name:/^[^\\\/:\*\?\"<>\|]+$/
    };
    this.callbacks=callbacks||{};
    this.Pass=true;//pass or not
	this.qs_keywords={
		"workspacename":{
			type:"string",
			content:"Untitled"
		},
		"viewname":{
			type:"reg",
			content:/^Share:.*/,
			examStr:"Share:****"
		}
	}; 
    this.validator={
		'required':	{patrn:this.Patrn.Required,		msg:""},
		'number':	{patrn:this.Patrn.Number,		msg:""},
		'integer':	{patrn:this.Patrn.Integer,		msg:""},
		'pint':		{patrn:this.Patrn.Pint,			msg:""},
		'double':	{patrn:this.Patrn.Double,		msg:""},
		'char':		{patrn:this.Patrn.Character,	msg:""},
		'nopun':	{patrn:this.Patrn.NoPun,		msg:""},
		'email':	{patrn:this.Patrn.Email,		msg:""},
		'password':	{patrn:this.Patrn.Password,		msg:""},
		//'zip':		{patrn:this.Patrn.ZipCode,		msg:""},
		'phone':	{patrn:this.Patrn.Phone,		msg:""},
		'ticker':	{patrn:this.Patrn.TickerName,	msg:"Sorry, only letters (a-z), numbers (0-9), and some symbols (@,!,:,$,.,-,_,/) are allowed!"},
		'name': 	{patrn:this.Patrn.Name,			msg:"Sorry, a name cannot contain any of the following characters:\\ / : * ? \" < > |"}
	}
}
Util.Validator.prototype = {
	validate:function(v,t){
		return !!this.validator[t].patrn.exec(v);
	},
	testKeyWord:function(str,keyword){
		var returnValue={valid:true,promprStr:""};
		if(!keyword) return returnValue;
		var keywordObj=this.qs_keywords[keyword];
		switch(keywordObj.type){
			case "string":
				if($.trim(str)==keywordObj.content){
					returnValue={valid:false,promprStr:keywordObj.content};
				}
				break;
			case "reg":
				if(!keywordObj.content.exec(str)){
					returnValue={valid:false,promprStr:keywordObj.examStr};
				}
				break;
			default:
				break;
		}
		return returnValue;
	},
    test:function(v,t){
		this.Pass=this.validate(v,t);
		if(!this.Pass){
			Util.message(this.validator[t].msg);
		}
		return this.Pass;
    },
	
	sliceInvalidChar:function(v,t){
		if(v==null||$.trim(v)=='')return "";
		else{
			for(var idx=v.length;idx>0;idx--){
				var rtn=v.substr(0,idx);
				if(this.validator[t].patrn.exec(rtn)!=null){
					return rtn;
				}
					
			}
			return "";
		}
	},
	
	validOnDOM:function(dom,t){
		dom.bind('',function(){
			
		});
	},
	
	
	
    handler:function(obj,n){
        this.Pass=false;
        if(typeof(this.callbacks.onError)== "function"){
            this.callbacks.onError(obj,n);
        }        
    },
	
    reset:function(){
        this.Pass=true;
    }
}

var QS_Validator=new Util.Validator({});